package cn.fanyamin.web.struts;

import java.util.List;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.ItemService;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.User;

public class CategoryAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(CategoryAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		ItemService itemService = getWebApp().getItemService();
		
		User user = getUserFromSession(request);
		List<UserCategory> list = itemService.getCategories(user.getUserID());
		request.setAttribute("categoryList", list);
        return mapping.findForward("list");

	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		ItemService itemService = getWebApp().getItemService();
		UserCategory category = getCategoryFromForm(form);
		if(category == null) {
		    return mapping.findForward("failure");
		}
		User user = getUserFromSession(request);
		category.setUserID(user.getUserID());
		int categoryID = itemService.createCategory(category);
		category.setCategoryID(categoryID);
		request.setAttribute("categoryType", category.getCategoryType());
		logger.info("create category: " + category);
		return mapping.findForward("success");
	}
	
    private UserCategory getCategoryFromForm(ActionForm form) {
    	UserCategory category = new UserCategory();
		LazyValidatorForm categoryForm = (LazyValidatorForm) form;
		category.setCategoryID(NumberUtils.toInt((String) categoryForm.get("categoryID")));
		category.setCategoryType(NumberUtils.toInt((String) categoryForm.get("categoryType")));
		category.setCategoryName(StringUtils.trim(""+categoryForm.get("categoryName")));	
		category.setDescription(StringUtils.trim(""+ categoryForm.get("description")));
		logger.info("category="+category);
		return category;
	}

/*    public ActionForward getCategory(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ItemService itemService = getWebApp().getItemService();
        LazyValidatorForm categoryFrom = (LazyValidatorForm)form;
        int categoryID = NumberUtils.toInt((String)categoryFrom.get("categoryID"));
        Category category = itemService.getCategory(categoryID);
        request.setAttribute("category", category);
        return mapping.findForward("edit");
    }*/
    
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        ItemService itemService = getWebApp().getItemService();

        UserCategory category = getCategoryFromForm(form);
        itemService.updateCategory(category);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ItemService itemService = getWebApp().getItemService();
        //LazyValidatorForm categoryFrom = (LazyValidatorForm)form;
        int categoryID = NumberUtils.toInt(request.getParameter("categoryID"));
        itemService.deleteCategory(categoryID);
        logger.info("delete category, categoryID=" + categoryID);
        return mapping.findForward("success");
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add category");
    	   	
    	return mapping.findForward("add");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ItemService itemService = getWebApp().getItemService();
        //LazyValidatorForm categoryFrom = (LazyValidatorForm)form;
        int categoryID = NumberUtils.toInt(request.getParameter("categoryID"));
        UserCategory category = itemService.getCategory(categoryID);
        request.setAttribute("category", category);
        return mapping.findForward("edit");
    }
}
